#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project : aimix
@File    : base_worker.py
@IDE     : PyCharm
@Author  : admin
@Date    : 2025/5/21 15:06
"""
from PyQt5.QtCore import QThread, pyqtSignal
from multiprocessing import Queue
from log import log


class BaseThread(QThread):
    """ Base class for all threads """
    failed = pyqtSignal(str)
    progress = pyqtSignal(str, int)

    def __init__(self, data: dict, parent=None):
        super(BaseThread, self).__init__(parent)
        self.process = None
        self.process_value = 0
        self.data = data
        self.queue = Queue()
        self.name = data["task"]
        self.max_progress = 95

    def on_change_progress(self, number):
        """ Override this method to perform the work """
        self.process_value += number
        self.progress.emit(self.name, min(self.process_value, self.max_progress))

    def run(self):
        """ Start the thread """
        pass

    def stop(self):
        """ Stop the thread """
        self.process_value = 0
        if self.process:
            log.debug("[主线程] 正在终止进程池")
            self.process.terminate()
            self.process.join()
            self.process = None
            log.debug("[主线程] 进程池已终止")
